package com.appBase.tools;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;

import com.appBase.R;
import com.wang.avi.AVLoadingIndicatorView;

public class LoadingDialog extends AlertDialog {

    private static LoadingDialog loadingDialog = null;
    private AVLoadingIndicatorView avi;
//    private static Context currentContext = null;
    private static int showCount = 0;

    public static LoadingDialog getInstance(Context context) {
        if (loadingDialog == null) {
//            currentContext = context;
            loadingDialog = new LoadingDialog(context, R.style.TransparentDialog); //设置AlertDialog背景透明
            loadingDialog.setCancelable(false);
            loadingDialog.setCanceledOnTouchOutside(false);
        }
        return loadingDialog;
    }

    public LoadingDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_loading);
        avi = (AVLoadingIndicatorView) this.findViewById(R.id.avi);
    }

    @Override
    public void show() {
        if (showCount<1) {
            super.show();
            avi.show();
        }
        showCount++;
    }

    @Override
    public void dismiss() {
        showCount--;
        if (showCount<=0) {
            super.dismiss();
//            currentContext = null;
            loadingDialog = null;
            avi.hide();
        }

    }
}
